{{!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

Generates external type annotations for legacy Thrift files.

}}
{{> common/AutoGeneratedPy}}
import typing as __T
{{#program:asyncio?}}
import asyncio
{{/program:asyncio?}}

from thrift import Thrift
from thrift.protocol.TProtocol import TProtocolBase

{{#program:includeNamespaces}}
{{#hasTypes?}}
import {{#includeNamespace}}{{value}}.{{/includeNamespace}}ttypes
{{/hasTypes?}}
{{/program:includeNamespaces}}
from {{#program:pyNamespaces}}{{value}}.{{/program:pyNamespaces}}ttypes import *
{{#service}}
{{#service:extends}}
import {{#service:pyNamespaces}}{{value}}.{{/service:pyNamespaces}}{{service:name}}
{{/service:extends}}


class Iface{{!
    }}{{#service:extends}}({{!
        }}{{#service:pyNamespaces}}{{value}}.{{/service:pyNamespaces}}{{!
            }}{{service:name}}.Iface{{!
    }}){{/service:extends}}:{{^service:functions}} ...{{/service:functions}}  # {{service:name}}
    {{#program:asyncio?}}
    # These have to be Awaitable because handler methods don't return Futures
    # Clients do return Futures but lsp requires us to say they are both Awaitable
    # You can cast the result of a Client if you need Future
    {{/program:asyncio?}}
    {{#service:functions}}
    def {{function:name}}(self{{#function:args}}, {{!
    }}{{field:name}}: {{#field:type}}{{> common/FieldType}}{{/field:type}}{{!
    }}{{/function:args}}) -> {{!
    }}{{#program:asyncio?}}__T.Awaitable[{{/program:asyncio?}}{{!
    }}{{#function:returnType}}{{> common/FieldType}}{{/function:returnType}}{{!
    }}{{#program:asyncio?}}]{{/program:asyncio?}}{{!
    }}: ...
    {{/service:functions}}

class Client(Iface, __T.ContextManager[Client]):  # {{service:name}}
    {{#program:asyncio?}}{{!
    }}def __init__(self, oprot: TProtocolBase, loop: __T.Optional[asyncio.AbstractEventLoop] = None) -> None: ...{{!
    }}{{/program:asyncio?}}{{!
    }}{{^program:asyncio?}}{{!
    }}def __init__(self, iprot: TProtocolBase, oprot: __T.Optional[TProtocolBase] = None) -> None: ...{{!
    }}{{/program:asyncio?}}
    {{#service:functions}}
    def send_{{function:name}}(self{{#function:args}}, {{!
    }}{{field:name}}: {{#field:type}}{{> common/OptionalFieldType}}{{/field:type}} = ...{{!
    }}{{/function:args}}) -> None: ...
    def recv_{{function:name}}(self{{#program:asyncio?}}, iprot: TProtocolBase, mtype: int, rseqid: int{{/program:asyncio?}}) -> {{!
    }}{{#program:asyncio?}}None{{/program:asyncio?}}{{!
    }}{{^program:asyncio?}}{{!
    }}{{#function:returnType}}{{> common/FieldType}}{{/function:returnType}}{{!
    }}{{/program:asyncio?}}{{!
    }}: ...
    {{/service:functions}}

class Processor(Iface, Thrift.TProcessor):  # {{service:name}}
    def __init__(self, handler: Iface) -> None:
        self._handler: Iface
        self._onewayMethods: __T.Sequence[__T.Callable]
        self._processMap: __T.Dict[str, __T.Callable]

    {{#service:functions}}
    def process_{{function:name}}(self, {{!
      }}seqid: int, {{!
      }}iprot: TProtocolBase, {{!
      }}oprot: TProtocolBase, {{!
      }}server_ctx: __T.Any = ...{{!
    }}) -> {{^program:asyncio?}}None{{/program:asyncio?}}{{#program:asyncio?}}asyncio.Future{{/program:asyncio?}}: ...
    {{/service:functions}}
    def process_main(self, {{!
      }}iprot: TProtocolBase, {{!
      }}oprot: TProtocolBase, {{!
      }}server_ctx: __T.Any = ...{{!
    }}) -> {{^program:asyncio?}}__T.Optional[bool]{{/program:asyncio?}}{{#program:asyncio?}}asyncio.Future{{/program:asyncio?}}: ...
    def onewayMethods(self) -> __T.Tuple[__T.Callable]: ...
{{#service:functions}}

class {{function:name}}_args:
    thrift_spec: __T.Tuple[__T.Optional[__T.Tuple[int, int, str, __T.Any, __T.Optional[int], int]]]
    thrift_field_annotations: __T.Dict[int, __T.Dict[str, str]]
    thrift_struct_annotations: __T.Dict[str, str]

    def __init__(
        self{{#function:args}},
        {{field:name}}: {{#field:type}}{{> common/OptionalFieldType}}{{/field:type}} = ...{{/function:args}}
    ) -> None:
        {{#function:args}}
        {{#field:type}}
        self.{{field:name}}: {{> common/OptionalFieldType}}
        {{/field:type}}
        {{/function:args}}
        {{^function:args}}
        ...
        {{/function:args}}

    def isUnion(self) -> bool: ...
    def checkRequired(self) -> None: ...
    def read(self, iprot: TProtocolBase) -> None: ...
    def write(self, oprot: TProtocolBase) -> None: ...
    def __eq__(self, other: __T.Any) -> bool: ...
    def __ne__(self, other: __T.Any) -> bool: ...

class {{function:name}}_result:
    thrift_spec: __T.Tuple[__T.Optional[__T.Tuple[int, int, str, __T.Any, __T.Optional[int], int]]]
    thrift_field_annotations: __T.Dict[int, __T.Dict[str, str]]
    thrift_struct_annotations: __T.Dict[str, str]

    def __init__(self, {{!
            }}success: {{#function:returnType}}{{> common/FieldType}}{{!
            }}{{/function:returnType}} = ...) -> None:
        self.success: {{#function:returnType}}{{> common/FieldType}}{{!
            }}{{/function:returnType}}

    def isUnion(self) -> bool: ...
    def checkRequired(self) -> None: ...
    def read(self, iprot: TProtocolBase) -> None: ...
    def write(self, oprot: TProtocolBase) -> None: ...
    def __eq__(self, other: __T.Any) -> bool: ...
    def __ne__(self, other: __T.Any) -> bool: ...
{{/service:functions}}
{{/service}}
